import 'package:flutter/material.dart';
import 'package:body_measurement/common/Iconfont.dart';
import 'package:body_measurement/common/measuring_api.dart';

class AsideNav extends StatefulWidget {
  const AsideNav(this.username, {super.key});

  final String username;

  @override
  State<AsideNav> createState() => _AsideNavState();
}

class _AsideNavState extends State<AsideNav> {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 72,
      height: double.infinity,
      child: DecoratedBox(
        decoration: const BoxDecoration(
          color: Color(0xffededed),
          border: Border(right: BorderSide(color: Color(0xffe6e6e6), width: 2.0)),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            _buildAvatar(),
            const Expanded(
              flex: 1,
              child: Column(
                children: <Widget>[],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 10.0),
              child: IconButton(
                onPressed: () {},
                icon: const Icon(IconFont.home, size: 28.0),
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 10.0),
              child: IconButton(
                onPressed: () {},
                icon: const Icon(IconFont.settings, size: 28.0),
              ),
            ),
            const Text(
              'v1.0.3',
              style: TextStyle(fontSize: 17.0),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAvatar() {
    return Padding(
      padding: const EdgeInsets.only(top: 10.0),
      child: CircleAvatar(
        backgroundColor: const Color(0xff409eff),
        minRadius: 32,
        child: TextButton(
          onPressed: () => handleLogout(context),
          child: Text(
            widget.username,
            style: const TextStyle(color: Colors.white),
            overflow: TextOverflow.ellipsis,
          ),
        ),
      ),
    );
  }

  void handleLogout(BuildContext context) {
    showDialog<String>(
      context: context,
      builder: (BuildContext context) => AlertDialog(
        title: const Text('登出'),
        content: const Text('确定登出当前账号吗？'),
        actions: <Widget>[
          TextButton(
            onPressed: () => Navigator.pop(context, 'Cancel'),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              MeasuringApi().logout();
              Navigator.of(context).pushNamed('login_page');
            },
            child: const Text('确定'),
          ),
        ],
      ),
    );
  }
}
